//
//  YFHomeOneCell.m
//  QuanJieShopProject
//
//  Created by 杨帆 on 2018/3/29.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFHomeOneCell.h"

@implementation YFHomeOneCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeView:frame];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)makeView:(CGRect)frame {
    
    // 图片
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.width)];
    [self addSubview:imgView];
    self.imageView = imgView;
    
    CGFloat totalH = frame.size.height - imgView.frame.size.height;
    CGFloat h = totalH/2.f;
    
    CGFloat padding = 10;
    // 标题
    CGFloat cellTitleW = frame.size.width - padding * 2;
    UILabel *cellTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, CGRectGetMaxY(imgView.frame), cellTitleW, h)];
    [self addSubview:cellTitleLabel];
    cellTitleLabel.font = [UIFont boldSystemFontOfSize:13];
    cellTitleLabel.numberOfLines = 2;
    cellTitleLabel.textColor = YFColor_Text;
    self.cellTitleLabel = cellTitleLabel;
    
    // 价格
    UILabel *cellPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, CGRectGetMaxY(cellTitleLabel.frame), frame.size.width, h)];
    [self addSubview:cellPriceLabel];
    cellPriceLabel.font = [UIFont systemFontOfSize:16];
    cellPriceLabel.textColor = YFColor_Red;
    self.cellPriceLabel = cellPriceLabel;
    
    // 购物车
    CGFloat rightPadding = 10;
    CGFloat shopCarBtnWH = h;
    CGFloat shopCarBtnX = frame.size.width - shopCarBtnWH - rightPadding;
    UIButton *shopCarBtn = [[UIButton alloc] initWithFrame:CGRectMake(shopCarBtnX, CGRectGetMaxY(cellTitleLabel.frame), shopCarBtnWH, shopCarBtnWH)];
    [shopCarBtn setImage:[UIImage imageNamed:@"shopCar"] forState:UIControlStateNormal];
    [shopCarBtn addTarget:self action:@selector(shopCarBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.shopCarBtn = shopCarBtn;
    [self addSubview:shopCarBtn];
}

- (void)shopCarBtnClick {
    NSLog(@"点击购物车");
}

- (void)cellUpdateWithModel:(SearchMdoel *)model {

    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:model.original_img];
    
    if (cachedImage) {
        self.imageView.image = cachedImage;
    }else {
        
//        NSLog(@"original_img = %@", [NSString stringWithFormat:@"%@%@",NET_BASEURL_IMG,model.original_img]);
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.original_img]] placeholderImage:[UIImage imageNamed:@"noImage"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
            if (!error) {
//                KMyLog(@"model.image_url = %@", [NSString stringWithFormat:@"%@%@",NET_BASEURL_IMG,model.image_url]);

                // 保存图片
                [[SDImageCache sharedImageCache] storeImage:image forKey:model.original_img completion:nil];
            }else {
                // http://api.quanjieshop.com/upload/shop/20180117/5a77b18b39f153fb20f2baf6b3d60bc8.jpg
//                KMyLog(@"model.image_url = %@", [NSString stringWithFormat:@"%@%@",NET_BASEURL_IMG,model.image_url]);
//                KMyLog(@"model.image_url = %@", model.image_url);

            }
        }];
    }
    
//    [self.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL_IMG,model.image_url]] placeholderImage:[UIImage imageNamed:@"noImage"]];
    
    self.cellTitleLabel.text = [NSString stringWithoutNilFormart:model.goods_name];
    self.cellPriceLabel.text = [NSString stringWithFormat:@"￥%@",model.shop_price];
    
}

@end
